extends ../layout

block content
  .container(style="margin-top:30px;")
    form#form.col-md-5.col-md-offset-3(method="post")
      .form-group
        label.sr-only(for="inputEmail") 邮箱地址／用户名
        input#inputEmail.form-control(type="text" name="account" placeholder="邮箱地址／用户名" required autofocus)
      .form-group
        .col-md-10(style="margin-left:-15px;")
          label.sr-only(for="inputToken") token
          input#inputToken.form-control(type="text" name="token" placeholder="token" required)
        .col-md-2
          a.form-control.btn.btn-link(style="margin-bottom:15px;" target="_blank" href="/auth/login") 获取token
      .form-group
        label.sr-only(for="inputPassword") 原密码
        input#inputPassword.form-control(type="password" name="oldPassword" placeholder="原密码" required)
      .form-group
        label.sr-only(for="inputNewPassword") 新密码
        input#inputNewPassword.form-control(type="password" name="newPassword" placeholder="新密码" required)
      .form-group
        a#submitBtn.btn.btn-lg.btn-primary.btn-block 修改密码

block js
  script().
    var submit = false;
    $('#submitBtn').on('click', function () {
      if (submit) {
        return ;
      }
      var token = $('#inputToken').val();
      var oldPassword = $('#inputPassword').val();
      var newPassword = $('#inputNewPassword').val();
      submit = true;
      $.ajax({
        type: 'patch',
        data: JSON.stringify({oldPassword:oldPassword,newPassword:newPassword}),
        contentType: 'application/json;charset=utf-8',
        headers: {
          Authorization : 'Bearer '+token
        },
        url: '/users/password',
        dataType: 'json',
        success: function (data) {
          if (data.status == "OK") {
            alert("修改成功");
            location.href = '/auth/login';
          } else if (data.status == 401) {
            alert('token invalid');
          } else {
            alert(data.message);
          }
          submit = false;
        }
      });
    });

